import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../route_component/Login.vue'
import Home from '../route_component/Home.vue'
import Welcome from '../route_component/Welcome.vue'
import Users from '../route_component/Users.vue'
import Rights from '../route_component/Rights.vue'
import Roles from '../route_component/Roles.vue'
import Goods_fenlei from '../route_component/Goods_fenlei.vue'
import Fei_lei_params from '../route_component/Fei_lei_params.vue'
import Goods_lists from '../route_component/Goods_lists.vue'
import Orders from '../route_component/Orders.vue'
import Add_goods from '../route_component/Add_goods.vue'
import Datainfo from '../route_component/DataInfo.vue'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect:'/login' },
    { path: '/login', component: Login },
    { path: '/home', component: Home, redirect: '/wlecome', children:[
      {path: '/wlecome', component: Welcome},
      {path: '/users', component: Users},
      {path: '/rights', component: Rights},
      {path: '/roles', component: Roles},
      {path: '/categories', component: Goods_fenlei},
      {path: '/params', component: Fei_lei_params},
      {path: '/goods', component: Goods_lists},
      {path: '/orders', component: Orders},
      {path: '/goods/add', component: Add_goods},
      {path: '/reports', component: Datainfo}
    ]}
  ]
})

router.beforeEach((to, from, next) => {
  const my_token = window.sessionStorage.getItem('token'); 
  if(to.path === '/login') {
    return next();
  }
  else {
    if(my_token) {
      return next();
    }else {
      return next('/login');
    }
  }
})

export default router
